/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2018-09-11 23:26:20
 * @LastEditTime: 2021-09-26 00:02:29
 * @LastEditors: vai-bowei
 */
/**
 * Created by 23hp on 2018/3/30.
 * 基于Promise的网络请求库,包含GET POST请求，上传下载功能
 * 使用方法：
 * 先引入： import {get,post,...} from 本文件;
 * · get请求:    get("/index",{id:2}).then(data=>{}).catch(error=>{});
 * · post请求:    post("/index",{id:2}).then(data=>{}).catch(error=>{});
 * Promise详细介绍：
 * http://es6.ruanyifeng.com/#docs/promise
 */
// import { API_CampusRoot } from './service';
import { transmitUrlToVisit } from "./proxy";

export const get = (url, data, headers) => request("GET", url, data, headers);
export const post = (url, data, headers) => request("POST", url, data, headers);
export const put = (url, data, headers) => request("PUT", url, data, headers);
export const del = (url, data, headers) => request("DELETE", url, data, headers);

export const transmitGet = (url, data, headers) => request("GET", transmitUrlToVisit(url), data, headers);
export const transmitPost = (url, data, headers) => request("POST", transmitUrlToVisit(url), data, headers);
export const transmitPostWithTimeout = transmitPost;
export const transmitPostForm = transmitPost;
export const transmitPut = (url, data, headers) => request("PUT", transmitUrlToVisit(url), data, headers);
export const transmitDel = (url, data, headers) => request("DELETE", transmitUrlToVisit(url), data, headers);

export function request(method, url, data, header = { "Content-Type": "application/json" }) {
    return new Promise((resolve, reject) => {
        try {
            wx.request({
                url,
                method,
                data,
                header,
                success: (res) => {
                    resolve(res.data);
                },
                fail: (error) => {
                    reject(error);
                },
                complete(complete) {
                },
            });
        } catch (err) {
            
        }
    }).catch((err) => {
        console.log('请求失败catch,',err);
    });
}
